package com.strelitzia.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strelitzia.entity.FriendsEntity;
import com.strelitzia.exception.StrelitziaException;
import com.strelitzia.filter.CorsFilter;
import com.strelitzia.service.FriendsService;
import com.strelitzia.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @create 2023/12/18 10:51
 */
@RestController
@RequestMapping("/friends")
public class FriendController {
    @Autowired
    private FriendsService friendsService;

    @GetMapping()
    public ResultVo getFriends() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(friendsService.getFriends());
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("获取好友列表失败");
        }
        return resultVo;
    }

//    @GetMapping("/{id}")
//    public ResultVo  getFriend(@PathVariable("id") Integer id) {
//        ResultVo resultVo = new ResultVo();
//        try {
//            resultVo.setData(friendsService.get(id));
//        } catch (Exception e) {
//            resultVo.setCode(1);
//            resultVo.setMsg("获取好友信息失败");
//        }
//        return resultVo;
//    }

    @GetMapping("/chatHistory/{friendId}")
    public ResultVo getChatHistory(@PathVariable Integer friendId) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(friendsService.getChatHistory(friendId));
        } catch (StrelitziaException e) {
            resultVo.setCode(1);
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }

    @GetMapping("/changeRemarks/{friendId}/{remark}")
    public ResultVo changeRemarks(@PathVariable Integer friendId, @PathVariable String remark) {
        ResultVo resultVo = new ResultVo();
        try {
            friendsService.changeRemarks(friendId, remark);
            resultVo.setMsg("修改备注成功");
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("修改备注失败");
        }

        return resultVo;
    }

    @GetMapping("/cacheFollow/{friendId}")
    public ResultVo cacheFollow(@PathVariable Integer friendId) {
        ResultVo resultVo = new ResultVo();
        try {
            friendsService.cacheFollow(friendId);
            resultVo.setMsg("取消关注成功");
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("取消关注失败");
        }
        return resultVo;
    }

    @GetMapping("/add/{friendId}")
    public void add(@PathVariable Integer friendId) {
        FriendsEntity friendsEntity = new FriendsEntity();
        friendsEntity.setRemark("");
        friendsEntity.setFriendId(friendId);
        friendsEntity.setUid(CorsFilter.loginUserId.get());
        if (friendsService.getOne(new QueryWrapper<FriendsEntity>().eq("uid", CorsFilter.loginUserId.get()).eq("friend_id", friendId)) == null)
            friendsService.save(friendsEntity);
    }

    @GetMapping("/fans")
    public Integer fans() {
        return friendsService.getFans();
    }

    @GetMapping("/follow/{id}")
    public Integer follow(@PathVariable("id") Integer id) {
        return friendsService.getFollow(id);
    }

    @GetMapping("/fans/{id}")
    public Integer fans(@PathVariable("id") Integer id) {
        return friendsService.getFans(id);
    }

    @GetMapping("/follow")
    public Integer follow() {
        return friendsService.getFollow();
    }
}
